<?php
/**
 * 我的服务类
 */
namespace app\index\service;

use Config;
use Cache;
use think\Db;

class Mine
{
	public function getMyLanded($member_id)
    {
        $landed = Db::table('zh_landed_property_order')
            ->alias('o')
            ->join('zh_landed_property p','o.property_id = p.id')
            ->join('zh_landed_property_floor f','o.floor_id = f.id')
            ->where('o.member_id',$member_id)
            ->where('o.status',2)
            ->field('o.pay_amount,p.name,p.thumb,f.build_num,f.floor,f.room_num,f.build_no,f.area')
            ->select();
        if(!empty($landed)){
            $landed = $this->setThumb($landed,0);
        }
        $second_house = Db::table('zh_landed_second_order')
            ->alias('o')
            ->join('zh_landed_second_house p','o.second_id = p.id')
            ->where('o.member_id',$member_id)
            ->where('o.status',2)
            ->field('o.pay_amount,p.name,p.thumb,p.on_floor,p.house,p.room_size')
            ->select();
        if(!empty($second_house)){
            $second_house = $this->setThumb($second_house,0);
        }
        $renting = Db::table('zh_landed_renting_order')
            ->alias('o')
            ->join('zh_landed_renting p','o.renting_id = p.id')
            ->where('o.member_id',$member_id)
            ->where('o.status',2)
            ->field('o.pay_amount,p.name,p.thumb,p.house,p.room_size,p.rent')
            ->select();
        if(!empty($renting)){
            $renting = $this->setThumb($renting,0);
        }
        $res = array_merge_recursive($landed,$second_house,$renting);
        foreach ($res as &$row) {
            if(isset($row['build_num']) && isset($row['floor']) && isset($row['build_no'])){
                $row['introduce'][] = $row['build_num'].'栋'.$row['floor'].$row['build_no'];
                $row['introduce'][] = $row['room_num'];
                $row['introduce'][] = $row['area'].'平方';
            }
            if(isset($row['on_floor']) && isset($row['house']) && isset($row['room_size'])){
                $row['introduce'][] = $row['on_floor'];
                $row['introduce'][] = $row['house'];
                $row['introduce'][] = $row['room_size'].'平方';
            }
            if(isset($row['house']) && isset($row['room_size']) && isset($row['rent'])){
                $row['introduce'][] = $row['house'];
                $row['introduce'][] = $row['room_size'].'平方';
                $row['introduce'][] = $row['rent'].'元/月';
            }
        }
        return $res;
    }


    /**
     * @param $member_id
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 获取我的申请商家
     */
    public function getApplyShop($member_id)
    {
        $res = Db::table('zh_store_apply')->where('member_id',$member_id)->select();
        return $res ? $res[0] : null ;
    }


    /**
     * @param $member_id
     * @param $param
     * @return bool
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     * 设置我的申请
     */
    public function setApplyShop($member_id,$param)
    {
        $res = $this->getApplyShop($member_id);
        if(!empty($res)){
            Db::table('zh_store_apply')->where('member_id',$member_id)->update($param);
        }else{
            Db::table('zh_store_apply')->insert($param);
        }
        return true;
    }



    /**
     *
     * 处理图片
     */
    public function setThumb($thumbs = [], $is_ad = 1)
    {
        if (!empty($thumbs) && is_array($thumbs)) {
            foreach ($thumbs as $key => &$value) {
                if (isset($value['thumb']) && !empty($value['thumb'])) {
//                    $value['thumb'] = 'http://'.$_SERVER['HTTP_HOST'].'/'.$value['thumb'];
                    if (isset($value['type']) && $is_ad == 1) {
                        $value['ad_type'] = self::$ad_type[$value['type']];
                        $value['thumb'] = 'http://yijiahoutai.40b876.cn/yijia_admin/public' . $value['thumb'];
                    } else {
                        $value['thumb'] = 'http://yijiahoutai.40b876.cn/yijia_store_admin/public' . $value['thumb'];
                    }
                }
                if (isset($value['logo']) && !empty($value['logo'])) {
//                    $value['logo'] = 'http://'.$_SERVER['HTTP_HOST'].'/'.$value['logo'];
                    $value['logo'] = 'http://yijiahoutai.40b876.cn/yijia_store_admin/public' . $value['logo'];
                }

            }
        }
        return $thumbs;
    }
}
